package utils;

/**
 * Created by fanzhenyu02 on 2020/2/14.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


public class ProcessZhongjin {


//    public static void main(String[] args) {
//        try {
//            FileInputStream inputStream = new FileInputStream("/Users/vonmax/Downloads/zhongjin.txt");
//            FileOutputStream outputStream = new FileOutputStream("/Users/vonmax/Downloads/zhongjin_process.txt");
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
//
//            Map<String, String> outNoDict = new TreeMap<>();
//
//            String str = null;
//            String curMsg = "", curOutNo = "";
//            int count = 0;
//            while ((str = bufferedReader.readLine()) != null) {
//                if (str.contains("bankCode:ZHONGJINDK")) {
//                    curMsg = str.split("message=")[1].split("\\{Content")[0] + "\n";
//                }
//
//                if (str.contains("outNo = ")) {
//                    curOutNo = str.split("outNo = ")[1].split(", ")[0];
//                    if (outNoDict.containsKey(curOutNo)) {
//                        System.out.println("Exisit outNo: " + curOutNo);
//                        continue;
//                    }
//
//
//                    String rawMsg = curMsg.split("&signature")[0].replaceAll("%2B", "\\+");
//                    String rawXml=new String(Base64.decode(rawMsg));
//
//
//
////                    System.out.println( + "\n\n\n");
//
//
//                    bufferedWriter.write(curOutNo + "\n" + curMsg + "\n\n");
//                    count++;
//                }
//            }
//
//            System.out.println("Total outNo count: " + count);
//            //close
//            inputStream.close();
//            bufferedReader.close();
//
//            bufferedWriter.close();
//            outputStream.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    public static void main(String[] args) {
        Map<String, String> dictBuy = new TreeMap<>();
        Map<String, String> dictCheckOff = new TreeMap<>();
        try {
            FileInputStream inputStreamBuy = new FileInputStream("/Users/vonmax/Downloads/buy2_Dict.txt");
            BufferedReader bufferedReaderBuy = new BufferedReader(new InputStreamReader(inputStreamBuy));
            FileInputStream inputStreamOff = new FileInputStream("/Users/vonmax/Downloads/checkoff2_Dict.txt");
            BufferedReader bufferedReaderOff = new BufferedReader(new InputStreamReader(inputStreamOff));

            String line = null;
            while ((line = bufferedReaderBuy.readLine()) != null) {
                //业务识别码,用户ID,金额（分）,业务线订单ID,业务线orderNo,统一支付给网关的outNo,1表示购买场景
                String outNo = line.split(",")[5];
                dictBuy.put(outNo, line);
            }

            while ((line = bufferedReaderOff.readLine()) != null) {
                //业务识别码,用户ID,金额（分）,业务线订单ID,业务线orderNo,统一支付给网关的outNo,5表示代扣
                String outNo = line.split(",")[5];
                dictCheckOff.put(outNo, line);
            }

            //close
            inputStreamBuy.close();
            bufferedReaderBuy.close();
            inputStreamOff.close();
            bufferedReaderOff.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(dictBuy);

        try {
            FileInputStream inputStream = new FileInputStream("/Users/vonmax/Downloads/fail.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String str = null;
            String curMsg = "", curOutNo = "";
            int count = 0;
            long moneySum = 0;
            while ((str = bufferedReader.readLine()) != null) {
                String amount = str.split(";")[1];
                moneySum += Long.valueOf(amount);
//                System.out.println(amount);
            }

            System.out.println("Total money sum: " + moneySum);
            //close
            inputStream.close();
            bufferedReader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

